#实现了窗口UI模型点击爬取数据20250306

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QMessageBox
from PyQt5.QtCore import pyqtSlot
import time
import re
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import csv
from datetime import datetime

class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('数据获取工具')
        self.setGeometry(100, 100, 800, 600) #参数100,100是窗口左上角在屏幕上的坐标，400和300是窗口的宽度和高度

        self.button = QPushButton('玩家列表', self)
        self.button.clicked.connect(self.on_button_clicked)
        self.button.move(150, 130)
        self.button.resize(100, 50)

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.button)
        self.setLayout(self.layout)

    @pyqtSlot()  # 修复：添加括号，表示无参数槽函数
    def on_button_clicked(self):
        self.fetch_data()

    def fetch_data(self):
        try:
            game_login_url = "https://ts1.x1.asia.travian.com/"
            base_count_list_url = "https://ts1.x1.asia.travian.com/statistics/player/overview?page="

            options = Options()
            options.add_argument("--disable-gpu")
            driver = webdriver.Chrome(options=options)
            driver.set_window_size(1200, 900)

            driver.get(game_login_url)
            time.sleep(3)  # 等待页面加载

            username_field = driver.find_element(By.NAME, "name")
            username_field.send_keys("小蜜蜂#bee")  # 替换为你的用户名
            time.sleep(1)

            password_field = driver.find_element(By.NAME, "password")
            password_field.send_keys("87654321")  # 替换为你的密码
            time.sleep(1)

            login_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
            login_button.click()
            time.sleep(3)  # 等待登录完成

            current_date = datetime.now().strftime("%Y%m%d")
            csv_filename = f"{current_date}.csv"

            with open(csv_filename, mode="w", newline="", encoding="utf-8") as file:
                writer = csv.writer(file)
                writer.writerow(["玩家", "统计时间", "玩家排名", "人口", "村莊"])

                page = 1
                while True:
                    count_list_url = f"{base_count_list_url}{page}"
                    driver.get(count_list_url)
                    time.sleep(2)  # 等待页面加载

                    try:
                        next_button = WebDriverWait(driver, 3).until(
                            EC.presence_of_element_located((By.XPATH, "//img[@class='next disabled']"))
                        )
                        if next_button:
                            print(f"到达最后一页：第 {page} 页")
                            break
                    except:
                        pass

                    WebDriverWait(driver, 3).until(
                        EC.presence_of_element_located((By.XPATH, "//tbody[@class='hoverable']"))
                    )
                    rows = driver.find_elements(By.XPATH, "//tbody[@class='hoverable']/tr[@class='hover']")
                    for row in rows:
                        try:
                            rank = re.sub(r'\u202e|\u202c', '', row.find_element(By.XPATH, ".//td[1]").text.strip())
                            player_name = re.sub(r'\u202e|\u202c', '', row.find_element(By.XPATH, ".//td[3]/a").text.strip())
                            population = re.sub(r'\u202e|\u202c', '', row.find_element(By.XPATH, ".//td[6]").text.strip().replace(",", ""))
                            villages = re.sub(r'\u202e|\u202c', '', row.find_element(By.XPATH, ".//td[7]").text.strip())

                            writer.writerow([player_name, current_date, rank, population, villages])
                        except Exception as e:
                            print(f"某些元素未找到：{e}")

                    page += 1

        except Exception as e:
            print(f"发生错误：{e}")
            QMessageBox.critical(self, "错误", f"发生错误：{e}")
        finally:
            driver.quit()
            QMessageBox.information(self, "完成", "数据获取完成")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    ex.show()
    sys.exit(app.exec_())